/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.ReserveList;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.ReserveListRepository;
import me.zhengjie.service.ReserveListService;
import me.zhengjie.service.dto.ReserveListDto;
import me.zhengjie.service.dto.ReserveListQueryCriteria;
import me.zhengjie.service.mapstruct.ReserveListMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ...
* @date 2020-11-08
**/
@Service
@RequiredArgsConstructor
public class ReserveListServiceImpl implements ReserveListService {

    private final ReserveListRepository reserveListRepository;
    private final ReserveListMapper reserveListMapper;

    @Override
    public Map<String,Object> queryAll(ReserveListQueryCriteria criteria, Pageable pageable){
        Page<ReserveList> page = reserveListRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reserveListMapper::toDto));
    }

    @Override
    public List<ReserveListDto> queryAll(ReserveListQueryCriteria criteria){
        return reserveListMapper.toDto(reserveListRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReserveListDto findById(Integer reserveListId) {
        ReserveList reserveList = reserveListRepository.findById(reserveListId).orElseGet(ReserveList::new);
        ValidationUtil.isNull(reserveList.getReserveListId(),"ReserveList","reserveListId",reserveListId);
        return reserveListMapper.toDto(reserveList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReserveListDto create(ReserveList resources) {
        return reserveListMapper.toDto(reserveListRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReserveList resources) {
        ReserveList reserveList = reserveListRepository.findById(resources.getReserveListId()).orElseGet(ReserveList::new);
        ValidationUtil.isNull( reserveList.getReserveListId(),"ReserveList","id",resources.getReserveListId());
        reserveList.copy(resources);
        reserveListRepository.save(reserveList);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer reserveListId : ids) {
            reserveListRepository.deleteById(reserveListId);
        }
    }

    @Override
    public void download(List<ReserveListDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReserveListDto reserveList : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("真实图书编号 ", reserveList.getRealBookId());
            map.put("预约时间 ", reserveList.getReserveTime());
            map.put("读者编号 ", reserveList.getReaderId());
            map.put("0:预约中 1:预约成功 2:预约失败 3:预约结束 4:交易中(自提方式)  5:交易中(物流方式) ", reserveList.getStatus());
            map.put("快递寄送编号 ", reserveList.getDeliveryId());
            map.put("0:在书易中流通   1：仅在图书馆 ", reserveList.getIsCirculate());
            map.put("持书者id ", reserveList.getHolderId());
            map.put("地址编号 关联预约者的地址 ", reserveList.getAddressId());
            map.put(" type",  reserveList.getType());
            map.put("模式 0自提 1:快递 2:面对面 ", reserveList.getMode());
            map.put("是否已支付订单:0，未支付 1,已支付 ", reserveList.getIsPay());
            map.put("是否启用 1启用 0未启用", reserveList.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}